<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入申请人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="1：未付款，2：已付款" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择1：未付款，2：已付款" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="1:未发货，2：已发货" prop="clearStatus">
        <el-select v-model="queryParams.clearStatus" placeholder="请选择1:未发货，2：已发货" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dgw:va:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dgw:va:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dgw:va:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['dgw:va:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <el-table v-loading="loading" height="700" :data="deptList"
                  @row-click="selectRow"
                  :row-style="rowStyle"
                  @selection-change="handleSelectionChange">
          <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
          <el-table-column label="乡镇(12)" align="center" prop="villagesTowns"/>
          <el-table-column label="耳标使用数量" align="center" prop="vaSum"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small "
                @click="getSum(scope.row)"

              >查看
              </el-button>
              <!--                    <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['dgw:vaccine:remove']"
                                  >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="5" :xs="24">
        <el-table v-loading="loading" height="700" :data="deptCunList"
                  @row-click="selectRow"
                  :row-style="rowStyle"
                  @selection-change="handleSelectionChange">
          <!--      <el-table-column label="养殖用户id" align="center" prop="userId" />-->
          <el-table-column label="乡镇(12)" align="center" prop="village"/>
          <el-table-column label="耳标使用数量" align="center" prop="vaSum"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small "
                @click="getSum(scope.row)"

              >查看
              </el-button>
              <!--                    <el-button
                                    size="mini"
                                    type="text"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.row)"
                                    v-hasPermi="['dgw:vaccine:remove']"
                                  >删除</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="14" :xs="24">
        <el-table v-loading="loading" :data="vaList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <!--      <el-table-column label="地址" align="center" prop="id" />-->
          <el-table-column label="申请人姓名" align="center" prop="name" />
          <el-table-column label="手机号" align="center" prop="phone" />
          <el-table-column label="育龄" align="center" prop="age" />
          <el-table-column label="申请数量" align="center" prop="vaSum" />
          <el-table-column label="入栏时间" align="center" prop="lairageTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.lairageTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="种类" align="center" prop="cowType" >
            <template slot-scope="scope">
              <span>{{scope.row.cowType==1?'肉牛':scope.row.cowType==2?'母牛':scope.row.cowType==3?'牛犊':'育肥牛'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请单号" align="center" prop="odd" />
          <el-table-column label="总金额" align="center" prop="money" />
          <el-table-column label="是否付款" align="center" prop="status" >
            <template slot-scope="scope">
              <span>{{scope.row.status==1?'未付款':'已付款'}}}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否发货" align="center" prop="clearStatus" >
            <template slot-scope="scope">
              <span>{{scope.row.clearStatus==1?'未发货':'已发货'}}}</span>
            </template>
          </el-table-column>
          <el-table-column label="申请人账号名称" align="center" prop="creatBy" />
          <el-table-column label="创建时间" align="center" prop="creatTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="地址" align="center" prop="address" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['dgw:va:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['dgw:va:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改耳标申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入申请人姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="育龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入育龄" />
        </el-form-item>
        <el-form-item label="申请数量" prop="vaSum">
          <el-input v-model="form.vaSum" placeholder="请输入申请数量" />
        </el-form-item>
        <el-form-item label="入栏时间" prop="lairageTime">
          <el-date-picker clearable size="small"
            v-model="form.lairageTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择入栏时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="1：肉牛，2：母牛,3：牛犊，4：育肥牛" prop="cowType">
          <el-select v-model="form.cowType" placeholder="请选择1：肉牛，2：母牛,3：牛犊，4：育肥牛">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请单号" prop="odd">
          <el-input v-model="form.odd" placeholder="请输入申请单号" />
        </el-form-item>
        <el-form-item label="总金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="1：未付款，2：已付款" prop="status">
          <el-select v-model="form.status" placeholder="请选择1：未付款，2：已付款">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="1:未发货，2：已发货" prop="clearStatus">
          <el-select v-model="form.clearStatus" placeholder="请选择1:未发货，2：已发货">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="申请人账号名称" prop="creatBy">
          <el-input v-model="form.creatBy" placeholder="请输入申请人账号名称" />
        </el-form-item>
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker clearable size="small"
            v-model="form.creatTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVa, getVa, delVa, addVa, updateVa, exportVa, queryVillage, queryCunVillage } from '@/api/dgw/va'

export default {
  name: "Va",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 耳标申请表格数据
      vaList: [],
      deptList:[],
      deptCunList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryCunParams:{
        villagesTowns:null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        status: null,
        clearStatus: null,
        address: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getQueryVillage()
  },
  methods: {
    /** 查询耳标申请列表 */
    getList() {
      /*this.loading = true;
      listVa(this.queryParams).then(response => {
        this.vaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });*/
    },
    getQueryVillage(){
      this.loading = true;
      queryVillage().then(response=>{
        console.log("乡镇：",response)
        this.deptList = response.data;
        this.loading = false;
      })
    },
    getSum(row){
    this.queryCunParams.villagesTowns=row.villagesTowns
        queryCunVillage(this.queryCunParams).then(response=>{
          this.deptCunList = response.data;
        })

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        age: null,
        vaSum: null,
        lairageTime: null,
        cowType: null,
        odd: null,
        money: null,
        status: null,
        clearStatus: null,
        creatBy: null,
        creatTime: null,
        address: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加耳标申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVa(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改耳标申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVa(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVa(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除耳标申请编号为"' + ids + '"的数据项？').then(function() {
        return delVa(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有耳标申请数据项？').then(() => {
        this.exportLoading = true;
        return exportVa(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
